package com.edu.servlet;

import com.edu.service.UserPostService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/user/forum/detail")
public class UserPostDetailServlet extends HttpServlet {
    private UserPostService postService = new UserPostService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取帖子ID
        String postId = request.getParameter("id");
        if (postId == null || postId.trim().isEmpty()) {
            response.sendRedirect(request.getContextPath() + "/user/forum");
            return;
        }

        try {
            // 获取帖子详情和回复列表
            Map<String, Object> post = postService.userGetPostDetail(Integer.parseInt(postId));
            List<Map<String, Object>> replies = postService.userGetPostReplies(Integer.parseInt(postId));

            if (post == null) {
                response.sendRedirect(request.getContextPath() + "/user/forum");
                return;
            }

            // 设置属性
            request.setAttribute("post", post);
            request.setAttribute("replies", replies);

            // 转发到详情页
            request.getRequestDispatcher("/user/forum/detail.jsp").forward(request, response);

        } catch (NumberFormatException e) {
            response.sendRedirect(request.getContextPath() + "/user/forum");
        }
    }
} 